//
//  GenearchListTableViewController.m
//  Eath
//
//  Created by mao on 11/6/14.
//  Copyright (c) 2014 yuhaya. All rights reserved.
//

#import "GenearchListTableViewController.h"
#import "GenearchListTableViewCell.h"
#import "GenearchAddViewController.h"
#import "Tool.h"
#import "User.h"
#import "StudentListTableViewController.h"


@interface GenearchListTableViewController ()

@end

@implementation GenearchListTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    [customLab setTextColor:[UIColor whiteColor]];
    [customLab setText:@"家长列表"];
    customLab.textAlignment = NSTextAlignmentCenter;
    customLab.font = [UIFont boldSystemFontOfSize:16];
    self.navigationItem.titleView = customLab;
    
    GenearchList * gen_list;
    if (self.purpose == 2) {
        self.cell_id = @"genearch_cell_id";
        UINib *nib = [UINib nibWithNibName:@"GenearchListTableViewCell" bundle:nil];
        [self.tableView registerNib:nib forCellReuseIdentifier:self.cell_id];
        gen_list = [[GenearchList alloc] initWithStuId:self.stu.identify];
    }else if (self.purpose == 1){
        self.cell_id = @"sub_genearch_cell_id";
        UINib *nib = [UINib nibWithNibName:@"SubGenearchListTableViewCell" bundle:nil];
        [self.tableView registerNib:nib forCellReuseIdentifier:self.cell_id];
        gen_list = [[GenearchList alloc] init];
    }else if (self.purpose == 3){
        self.cell_id = @"sub_genearch_cell_id";
        UINib *nib = [UINib nibWithNibName:@"SubGenearchListTableViewCell" bundle:nil];
        [self.tableView registerNib:nib forCellReuseIdentifier:self.cell_id];
        gen_list = [[GenearchList alloc] init];
    }else if (self.purpose == 4){
        self.cell_id = @"sub_genearch_cell_id";
        UINib *nib = [UINib nibWithNibName:@"SubGenearchListTableViewCell" bundle:nil];
        [self.tableView registerNib:nib forCellReuseIdentifier:self.cell_id];
        gen_list = [[GenearchList alloc] init];
    }
    self.genList = gen_list;
    self.genList.detegate = self;
    self.genearchListData = [self.genList getGenearchList];
    
    
    if(self.purpose == 3){
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(addStuGen)];
    }else if(self.purpose == 1){
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(addGen)];
    }else{
//        if(self.stu.main){
//            self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addGen)];
//        }
    }
}

- (void) gens_load_fail
{

}

- (void) gens_load_over
{
    DLog(@"%@",[self.genList getGenearchList]);
    self.genearchListData = [self.genList getGenearchList];
    [self.tableView reloadData];
}

- (void)addGen
{
    GenearchAddViewController * addGen = [[GenearchAddViewController alloc] init];
    addGen.purpose = 3;
    [self.navigationController pushViewController:addGen animated:YES];
//    GenearchListTableViewController * selectGen = [[GenearchListTableViewController alloc] init];
//    selectGen.stu = self.stu;
//    selectGen.purpose = 3;
//    [self.navigationController pushViewController:selectGen animated:YES];
}

-(void)addStuGen
{
    if (self.gen == nil) {
        [Tool showMessage:@"请选择家长!"];
        return;
    }
    GenearchAddViewController * addStuGen = [[GenearchAddViewController alloc] init];
    addStuGen.gen = self.gen;
    addStuGen.purpose = 1;
    [self.navigationController pushViewController:addStuGen animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.purpose == 1 || self.purpose == 3) {
        return 70;
    }else{
        return 100;
    }
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [self.genearchListData count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GenearchListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cell_id forIndexPath:indexPath];
    Genearch * gen = self.genearchListData[indexPath.row];
    cell.name.text = gen.name;
    cell.photo.image = [UIImage imageNamed:gen.photo];
    cell.phone.text = gen.phone;
    
    if(self.purpose == 2){
        cell.relation.text = gen.relation;
        cell.main.text = gen.main ? @"主家长" : @"副家长";
    }
    
    return cell;
}

// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}


// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
//        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        
        if(self.purpose == 1){
            
            Genearch * gen = self.genearchListData[indexPath.row];
            NSString * gen_id = gen.identify;
            [self delGen:gen_id];
            
//            [Tool showMessage:@"删除副家长！"];
            
        }else if (self.purpose == 2){
            [Tool showMessage:@"删除家长与学生的对应关系"];
        }
        
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}

- (void)delGen:(NSString *)num
{
    if(![User isLogin]){
        [Tool showMessage:@"请登录!"];
        return;
    }
    NSString * token = [User token];
    NSDictionary * userinfo = [User loginInfo];
    NSString * par_user_id = userinfo[@"id"];
    NSDictionary * parameters = [[NSDictionary alloc] initWithObjectsAndKeys:par_user_id,@"par_user_id",num,@"vice_user_id", nil];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    NSString * url_stus_get = [NSString stringWithFormat:@"%@?token=%@",URL_VICE_USER_DEL,token];
    [manager POST:url_stus_get parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary * redata = responseObject;
//        NSArray * data = redata[@"data"];
        NSString * code = redata[@"code"];
        
        if(code.intValue  == 0){
//            DLog(@"%@",data);
            
            [self.tableView reloadData];
            
        }else{
            [Tool showMessage:redata[@"msg"]];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [Tool showMessage:[error localizedDescription]];
    }];
    
    
}



#pragma mark - Table view delegate

// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if(self.purpose == 3){
        self.gen = self.genearchListData[indexPath.row];
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else if(self.purpose == 4){
        
        self.gen = self.genearchListData[indexPath.row];
        StudentListTableViewController * stu_list = [[StudentListTableViewController alloc] init];
        stu_list.gen_id = self.gen.identify;
        DLog(@"%@",stu_list.gen_id);
        stu_list.operate = 5;
        [self.navigationController pushViewController:stu_list animated:YES];
    }else{
        GenearchAddViewController * genEdit = [[GenearchAddViewController alloc] init];
        genEdit.purpose = self.purpose;
        genEdit.gen = self.genearchListData[indexPath.row];
        [self.navigationController pushViewController:genEdit animated:YES];
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.purpose == 3) {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
}

- (void)viewWillAppear:(BOOL)animated;
{
    [self.genList network_gens];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
